package com.ease.context.jmx;

import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.springframework.jmx.export.MBeanExporterListener;

/**
 * 
 * @author nathanleewei
 * 
 */
public class HttpAdaptorExporterListener implements MBeanExporterListener {

	private MBeanServer mbeanServer;

	private String adaptorName = "com.irm.management:type=HttpAdaptor";

	public void mbeanRegistered(ObjectName objectName) {
		if (adaptorName.equals(objectName.getCanonicalName())) {
			try {
				mbeanServer.invoke(objectName, "start", null, null);
			} catch (Exception e) {
				System.err.println("Can't start HttpAdaptor: " + e);
			}
		}
	}

	public void mbeanUnregistered(ObjectName objectName) {
		if (adaptorName.equals(objectName.getCanonicalName())) {
			try {
				mbeanServer.invoke(objectName, "stop", null, null);
			} catch (Exception e) {
				System.err.println("Can't stop HttpAdaptor: " + e);
			}
		}
	}

	public void setMbeanServer(MBeanServer mbeanServer) {
		this.mbeanServer = mbeanServer;
	}

	public void setAdaptorName(String adaptorName) {
		this.adaptorName = adaptorName;
	}
}
